//
//  LGSportsEvaInputCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/4.
//  运动评估--填写Cell

#import "LGSportsEvaInputCell.h"

#define itemWidth    (Screen_W - viewPix(32))/2.0
#define itemHeight   viewPix(44)

@interface LGSportsEvaInputCell()<UITextFieldDelegate>
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGSportsEvaInputCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.baseView];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(10));
            make.left.equalTo(self.contentView).offset(viewPix(32));
            make.right.equalTo(self.contentView).offset(-viewPix(32));
        }];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(0);
            make.left.right.bottom.equalTo(self.contentView);
            make.height.equalTo(@(itemHeight));
        }];
    }
    return  self;
}

-(void)setResultDic:(NSDictionary *)resultDic{
    _resultDic = resultDic;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    
    for (UIView *itemView in self.itemArray) {
        [itemView removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    
    NSString *title = LGNSString(dataDic[@"title"]);
    self.titleLabel.text = title;
    CGFloat topMargin = (title.length > 0)?viewPix(38):0;
    CGFloat bottom = topMargin+itemHeight;
    NSArray *itemArray = dataDic[@"items"];
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSDictionary *tempDic = itemArray[i];
        UIView *itemView = [self itemViewWithTitle:tempDic index:i];
        itemView.frame = CGRectMake(itemWidth*(i%2)+viewPix(17), topMargin+itemHeight*(i/2), itemWidth, itemHeight);
        [self.contentView addSubview:itemView];
        [self.itemArray addObject:itemView];
        bottom = kMaxY(itemView.frame);
    }
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(bottom));
    }];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSInteger index = textField.tag-1000;
    NSDictionary *tempDic = self.dataDic[@"items"][index];
    NSInteger maxCount = [tempDic[@"max_length"] integerValue];
    textField.text = (textField.text.length>maxCount)?[textField.text substringToIndex:maxCount]:textField.text;
    if(self.delegate && [self.delegate respondsToSelector:@selector(inputContent:key:)]){
        [self.delegate inputContent:textField.text key:LGNSString(tempDic[@"name"])];
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *content = @"0123456789.-";
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:content] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
    }
    return _baseView;
}

-(UIView *)itemViewWithTitle:(NSDictionary *)dataDic  index:(NSInteger)index{
    UIView *baseView = [[UIView alloc]init];
    NSString *key = LGNSString(dataDic[@"name"]);
    NSString *title = LGNSString(dataDic[@"title"]);
    UILabel *titleLabel = [UILabel lableWithText:title colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:titleLabel];
    UITextField *textField = [[UITextField alloc]init];
    textField.backgroundColor = [UIColor colorWithString:@"#EFEFEF"];
    textField.tintColor = [UIColor colorWithString:@"#666666"];
    textField.textColor = [UIColor colorWithString:@"#666666"];
    textField.textAlignment = NSTextAlignmentLeft;
    if([key isEqualToString:@"v_17"] || [key isEqualToString:@"v_18"]){
        textField.keyboardType = UIKeyboardTypeNumberPad;
        
    }else if ([key isEqualToString:@"v_3"] || [key isEqualToString:@"v_4"]||[key isEqualToString:@"v_5"] || [key isEqualToString:@"v_6"]){
        //可输入负数
        textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }else{
        textField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    textField.cornerRidus = 7;
    textField.tag = 1000+index;
    textField.font = LGFontWeight(15, UIFontWeightMedium);
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(10), viewPix(32))];
    textField.rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(10), viewPix(32))];
    [textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    textField.delegate = self;
    [baseView addSubview:textField];
    if([self.resultDic.allKeys containsObject:key]){
        textField.text = LGNSString(self.resultDic[key]);
    }
    CGFloat textWidth = [title textWidthWithFont:LGFont(15) maxHeight:MAXFLOAT];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(baseView).offset(viewPix(15));
        make.centerY.equalTo(textField);
        make.width.equalTo(@(textWidth+3));
    }];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(viewPix(5));
        make.right.equalTo(baseView).offset(-viewPix(5));
        make.top.equalTo(baseView).offset(viewPix(3));
        make.height.equalTo(@(viewPix(32)));
    }];
    return baseView;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
